﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysArea
    {
        public int Add(Chain.Model.SysArea model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysArea(");
            builder.Append("PID,Name)");
            builder.Append(" values (");
            builder.Append("@PID,@Name)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PID", SqlDbType.Int, 4), new SqlParameter("@Name", SqlDbType.NVarChar, 100) };
            cmdParms[0].Value = model.PID;
            cmdParms[1].Value = model.Name;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysArea ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string IDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysArea ");
            builder.Append(" where ID in (" + IDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysArea");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int PID, string Name)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select count(1) from SysArea");
            builder.Append(" where PID=@PID and Name=@Name ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("PID", SqlDbType.Int, 4), new SqlParameter("Name", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = PID;
            cmdParms[1].Value = Name;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int ID, int PID, string Name)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select count(1) from SysArea");
            builder.Append(" where ID not in (@ID) and  PID=@PID and Name=@Name ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("ID", SqlDbType.Int, 4), new SqlParameter("PID", SqlDbType.Int), new SqlParameter("Name", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = ID;
            cmdParms[1].Value = PID;
            cmdParms[2].Value = Name;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,PID,Name ");
            builder.Append(" FROM SysArea ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ID,PID,Name ");
            builder.Append(" FROM SysArea ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ID desc");
            }
            builder.Append(")AS Row, T.*  from SysArea T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SysArea";
            string[] column = new string[] { "SysArea.*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ID", "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "SysArea");
        }

        public Chain.Model.SysArea GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ID,PID,Name from SysArea ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            Chain.Model.SysArea area = new Chain.Model.SysArea();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ID"] != null) && (set.Tables[0].Rows[0]["ID"].ToString() != ""))
            {
                area.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PID"] != null) && (set.Tables[0].Rows[0]["PID"].ToString() != ""))
            {
                area.PID = int.Parse(set.Tables[0].Rows[0]["PID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Name"] != null) && (set.Tables[0].Rows[0]["Name"].ToString() != ""))
            {
                area.Name = set.Tables[0].Rows[0]["Name"].ToString();
            }
            return area;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysArea ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.SysArea model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysArea set ");
            builder.Append("PID=@PID,");
            builder.Append("Name=@Name");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PID", SqlDbType.Int, 4), new SqlParameter("@Name", SqlDbType.NVarChar, 100), new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.PID;
            cmdParms[1].Value = model.Name;
            cmdParms[2].Value = model.ID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return -1;
        }
    }
}

